package product

import (
	"github.com/gin-gonic/gin"
	request2 "mallgo/internal/common/request"
	"mallgo/internal/common/response"
	"mallgo/internal/resources/product/api/domain/request"
	"mallgo/internal/resources/product/resource"
	"net/http"
)

// 商品增删改查控制器
type ProductController struct {
	productResource *resource.ProductResource
}

func NewProductController(productResource *resource.ProductResource) *ProductController {
	return &ProductController{productResource}
}

func (h *ProductController) Create(c *gin.Context) {
	// 绑定并验证请求数据
	var req request.ProductRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(200, response.Error(501, "请求参数错误"))
		return
	}
	res := h.productResource.Create(&req)
	c.JSON(http.StatusOK, res)

}

func (h *ProductController) Update(c *gin.Context) {
	// 绑定并验证请求数据
	var req request.ProductRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(200, response.Error(501, "请求参数错误"))
		return
	}
	res := h.productResource.Update(&req)
	c.JSON(http.StatusOK, res)
}

func (h *ProductController) Delete(c *gin.Context) {
	// 绑定并验证请求数据
	var req request.ProductRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(200, response.Error(501, "请求参数错误"))
		return
	}
	res := h.productResource.Delete(&req)
	c.JSON(http.StatusOK, res)
}

func (h *ProductController) Search(c *gin.Context) {
	// 绑定并验证请求数据
	var req request.ProductSearch
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(200, response.Error(501, "请求参数错误"))
		return
	}
	res := h.productResource.Search(&req)
	c.JSON(http.StatusOK, res)
}

func (h *ProductController) View(c *gin.Context) {
	// 绑定并验证请求数据
	var req request2.ViewRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(200, response.Error(501, "请求参数错误"))
		return
	}
	res := h.productResource.View(&req)
	c.JSON(http.StatusOK, res)
}
